﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAO;

namespace TAM_SPORT
{
    public partial class DebitColecttion : Form
    {
        private int id;
        public DebitColecttion()
        {
            InitializeComponent();
        }
        public DebitColecttion(int id)
        {
            InitializeComponent();
            this.id = id;
            onload(id);
        }
        public void loadImageCapture(string filename)
        {
            string path = filename;
            if (!String.IsNullOrEmpty(path))
            {
                try
                {
                    ptbImage.LoadAsync(path);
                    ptbImage.SizeMode = PictureBoxSizeMode.Zoom;
                    ptbImage.Text = filename;
                }
                catch (Exception ex)
                {
                    ptbImage.Image = null;
                    Console.WriteLine(ex);
                }
            }
            else
            {
                ptbImage.Image = null;
            }
        }
        private void onload(int id)
        {
            PurchaseDao dao = new PurchaseDao();
            SalesDao sdao = new SalesDao();
            DataRow row = dao.getCustomerHist(id);
            if (row != null)
            {
                txtName.Text = row["NAME"].ToString();
                rtbNote.Text = row["NOTE"].ToString();
                loadImageCapture(row["PICTURE"].ToString());
            }
            DataTable table = sdao.getDebitCustomerById(id);
            dtgCustomers.Rows.Clear();
            if (table != null && table.Rows.Count > 0)
            {
                foreach (DataRow row1 in table.Rows)
                {
                    int i = dtgCustomers.Rows.Add();
                    dtgCustomers.Rows[i].Cells[0].Value = i + 1;
                    dtgCustomers.Rows[i].Cells[1].Value = DateTime.Parse(row1["SALE_DATE"].ToString()).ToString("dd/MM/yyyy");
                    dtgCustomers.Rows[i].Cells[2].Value = row1["NAME"].ToString();
                    dtgCustomers.Rows[i].Cells[3].Value = ((double)row1["SO_LUONG"]).ToString("#,###.##");
                    dtgCustomers.Rows[i].Cells[4].Value = ((double)row1["TOTAL"]).ToString("#,###.##");
                    dtgCustomers.Rows[i].Cells[5].Value = ((double)row1["DEBIT_MONEY"]).ToString("#,###.##");
                    dtgCustomers.Rows[i].Cells[7].Value = row1["ID"];
                    if (i % 2 == 1)
                    {
                        dtgCustomers.Rows[i].DefaultCellStyle.BackColor = Color.LightCyan;
                    }
                }
            }
        }

        private void dtgCustomers_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex == 6)
            {
                int id = int.Parse(dtgCustomers.Rows[e.RowIndex].Cells[7].Value.ToString());
                Confirm con = new Confirm("Bạn muốn thu tiền ?");
                con.ShowDialog();
                if (con.IsAccept)
                {
                    SalesDao sdao = new SalesDao();
                    if (sdao.debitCollect(id))
                    {
                        //Result r = new Result("Thu tiền thành công.");
                        //r.ShowDialog();
                        //r.Dispose();
                        onload(id);
                    }
                    else
                    {
                        Error er = new Error("Xóa thất bại.");
                        er.ShowDialog();
                        er.Dispose();
                    }
                }
            }
        }

    }
}
